# -*- coding: UTF-8 -*-
__license__ = "GPL http://www.gnu.org/licenses/gpl.txt"
__author__="xushengs@gmail.com"
__homepage__ = "http://fdream.net"
__date__ ="2010-2-26 17:39:27"
__version = "0.1"

from config import config
from lib.usercookie import usercookie
from lib.gtwtools import gtwtools

#import logging
#from urllib import quote, urlencode
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
#from google.appengine.api import urlfetch

#from django.utils import simplejson as json

class User(webapp.RequestHandler):
    def get(self):
        process(self, method='get')
    def post(self):
        process(self, method='post')

def process(handler, method):
    request = handler.request
    response = handler.response
    
    tokens = None
    if config.cookie_name in request.cookies:
        tokens = usercookie.decrypt(request.cookies[config.cookie_name])

    if tokens is None:
        response.headers['Location'] = '/login/'
        response.set_status(302)
        return
    
    paths = request.path.replace('/', ' ').split()
    username = paths[0]
    module = ''
    function = ''
    if len(paths) > 1:
        module = paths[1]
    
    if len(paths) > 2:
        function = paths[2]
    
    # fetch user profile
    url = config.api + 'users/show.' + config.format
    params = gtwtools.get_oauth_params(base_url = url, params = {'oauth_token': tokens['oauth_token']}, temp_params = {'screen_name': username}, token_secret = tokens['oauth_token_secret'], method = 'GET')
    user = gtwtools.get_json(url=url + '?screen_name=' + username, headers={'Authorization': 'OAuth ' + gtwtools.dict2qscomma(params)}, method='GET')
    if user is None:
        response.out.write('访问Twitter出错，可能是Google App Engine或Twitter出现了问题，请稍后再试。')  
        return
    
    if module == 'lists':
        if function == 'memberships':
            return
        else:
            return
    elif module == 'friends':
        return
    elif module == 'follwers':
        return
    elif module == 'favorites':
        return
    else:
        tpl = config.tpl_path + 'user.html'
        url = config.api + 'statuses/user_timeline.' + config.format
        params = gtwtools.get_oauth_params(base_url = url, params = {'oauth_token': tokens['oauth_token']}, temp_params = {'screen_name': username, 'count': config.pagesize}, token_secret = tokens['oauth_token_secret'], method = 'GET')
        tweets = gtwtools.get_json(url=url + '?screen_name=' + username + '&count=' + str(config.pagesize), headers={'Authorization': 'OAuth ' + gtwtools.dict2qscomma(params)}, method='GET')
        if tweets is None:
            #response.out.write('访问Twitter出错，可能是Google App Engine或Twitter出现了问题，请稍后再试。')
            tweets = {}
            #return
        
        user['profile_image_url'] =  user['profile_image_url'].replace('_normal.', '_bigger.')
        response.out.write(template.render(tpl, {'user': user, 'tweets': tweets}))
    